<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2021/3/9 0009 14:18
 * Describe: 企业微信模块 - 素材管理
 */

namespace app\common\server\corp;

class Media extends Base
{
    /**
     * @var string 上传临时素材url
     */
    private $uploadTempMediaUrl = 'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s';

    /**
     * 上传临时素材
     * @link https://work.weixin.qq.com/api/doc/90001/90143/90389
     */
    public function uploadTempMedia(string $authCorpAccessToken, $type = 'image')
    {
        trace(222222);
        trace($authCorpAccessToken);
        $res = $this->curlFnFile(sprintf($this->uploadTempMediaUrl, $authCorpAccessToken, $type), 'POST', ['media' => '@abc.png']);
        $temp = $this->setResponse($res);
        trace(3333333);
        trace($temp);
        if(isset($temp['media_id'])){
            return $temp['media_id'];
        }
        check_exception($temp['errmsg']?? '上传临时素材失败');
    }

    /**
     * curl请求
     * -Baidu代码
     * @link https://blog.csdn.net/weng_xianhu/article/details/79419845
     */
    private function curlFnFile($url, $method = "POST", $postfields)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
        switch ($method) {
            case "POST":
                curl_setopt($ch, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    $hadFile = false;
                    if (is_array($postfields) && isset($postfields['media'])) {
                        /* 支持文件上传 */
                        if (class_exists('\CURLFile')) {
                            curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
                            foreach ($postfields as $key => $value) {
                                if ($this->isPostHasFile($value)) {
                                    $postfields[$key] = new \CURLFile(realpath(ltrim($value, '@')));
                                    $hadFile = true;
                                }
                            }
                        } elseif (defined('CURLOPT_SAFE_UPLOAD')) {
                            if ($this->isPostHasFile($value)) {
                                curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
                                $hadFile = true;
                            }
                        }
                    }
                    $tmpdatastr = (!$hadFile && is_array($postfields)) ? http_build_query($postfields) : $postfields;
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $tmpdatastr);
                }
                break;
            default:
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
                break;
        }
        $ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE;
        curl_setopt($ch, CURLOPT_URL, $url);
        if($ssl){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
        }
        $response =  curl_exec($ch);
        curl_close($ch);
        if(empty($response)){
            check_exception('上传临时素材失败');
        }
        return json_decode($response, true);
    }

    /**
     * curl请求
     * -Baidu代码
     * @link https://blog.csdn.net/weng_xianhu/article/details/79419845
     */
    public function isPostHasFile($value)
    {
        if (is_string($value) && strpos($value, '@') === 0 && is_file(realpath(ltrim($value, '@')))) {
            return true;
        }
        return false;
    }

}